package com.docios.cookbook2.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.docios.cookbook2.R;
import com.docios.cookbook2.entity.Category;

import java.util.List;

public class ItemCategoryAdapter extends RecyclerView.Adapter<ItemCategoryAdapter.ViewHolder> {

    List<Category> categories;
    Context catContext;

    public ItemCategoryAdapter(List<Category> categories,Context catContext){
        this.categories=categories;
        this.catContext=catContext;
    }

    @NonNull
    @Override
    public ItemCategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(catContext).inflate(R.layout.item_category_title, parent, false);
        return new ViewHolder(view);
    }

    /*
    * MAIN
    * */
    @Override
    public void onBindViewHolder(@NonNull ItemCategoryAdapter.ViewHolder holder, int position) {
        holder.title.setText(categories.get(position).getName());
        //子标签放入嵌套的 recyclerView 中
        ChildCategoryAdapter childAdapter=new ChildCategoryAdapter(categories.get(position).getLists(),catContext);
        holder.recycle_child.setAdapter(childAdapter);
        holder.recycle_child.setLayoutManager(new GridLayoutManager(catContext,4));
    }

    @Override
    public int getItemCount() {
        return categories.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        RecyclerView recycle_child;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            title=itemView.findViewById(R.id.tv_cat_title);
            recycle_child=itemView.findViewById(R.id.cat_recycle_child);
        }
    }
}
